//
//  YHWaterFlowLayout.m
//  UI15_CustomCollectionViewLayout
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "YHWaterFlowLayout.h"

@interface YHWaterFlowLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;
@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, assign) CGFloat contentHeight;


@end

@implementation YHWaterFlowLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        self.attributesArray = [NSMutableArray array];
        self.numberOfColumns = 2;
        self.contentHeight = 0.0f;
        self.cellMargin = 5.0f;
    }
    return self;
}


- (CGFloat)itemWidth {
    CGFloat allMargin = (self.numberOfColumns + 1) * self.cellMargin;
    return (self.collectionView.bounds.size.width - allMargin) / self.numberOfColumns;
}

// 自定义layout 必须重写三个方法
// 1. 准备布局 (计算每个item大小)
- (void)prepareLayout {
//    (调用时机刷新时 ) 清空内容高度
    self.contentHeight = 0.0f;
    if (self.attributesArray.count != 0) {
        [self.attributesArray removeAllObjects];
    }
    
    // 高度最小列
    NSInteger column = 0;
    // 存储每一列的高度
    NSMutableArray *columnOfHeightArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.numberOfColumns; i++) {
        // 默认高度为间距
        [columnOfHeightArray addObject:@(self.cellMargin)];
    }
    // 遍历collectionVIew中0区的所有item
    for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        // 给每个item进行布局(创建系统需要的布局描述类)
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        // 最小列的起始位置 (添加间距)
        CGFloat x = (self.itemWidth + self.cellMargin) * column + self.cellMargin;
        // 高度数组中最小列对应的高度
        CGFloat y = [columnOfHeightArray[column] floatValue];
        // 宽是根据列数求出的
        CGFloat width = self.itemWidth;
        // 高是通过代理方法求出的
        CGFloat height = [self.delegate collectionView:self.collectionView layout:self widtgh:self.itemWidth heightForItemAtIndexPath:index];
        attributes.frame = CGRectMake(x, y, width, height);
        [self.attributesArray addObject:attributes];
        // 更新数组中对应最小列高度
        columnOfHeightArray[column] = @([columnOfHeightArray[column] floatValue] + height + self.cellMargin);
        // 整个内容的最大高度
        self.contentHeight = MAX(self.contentHeight, [columnOfHeightArray[column] floatValue]);
        // 求出最小列
        for (NSInteger i = 0; i < columnOfHeightArray.count; i++) {
            CGFloat currentHeight = [columnOfHeightArray[column] floatValue];
            CGFloat height = [columnOfHeightArray[i] floatValue];
            if (currentHeight > height) {
                column = i;
                // 获取最大的高度赋值给滚动范围
//                self.contentHeight = currentHeight;
//            } else {
//                self.contentHeight = height;
            }
        }
    }
}

    

// 2. 返回的是每个item对应的布局属性
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    return self.attributesArray;
}

// 3. 负责collectionVIew的滚动范围
- (CGSize)collectionViewContentSize {
    return CGSizeMake(0, self.contentHeight);
}

@end
